﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Web.Configuration;
using System.Web.UI.WebControls;

/// <summary>
/// SessionObject 的摘要描述
/// </summary>
public class SessionObject : System.Web.UI.Page
{
    public SessionObject()
    {
        
    }

        /// <summary>
    /// 員工是否登入
    /// </summary>
    public bool SessionUserLogin
    {
        get
        {
            return Session["UserLogin"] == null ? false : (bool)Session["UserLogin"];
        }
        set
        {
            Session.Add("UserLogin", value);
        }
    }

    /// <summary>
    /// 員工編號
    /// </summary>
    public int SessionUserID
    {
        get
        {
            return Session["SessionUserID"] == null ? 0 : Convert.ToInt32(Session["SessionUserID"]);
        }
        set
        {
            Session.Add("SessionUserID", value);
        }
    }

    public string SessionUserName
    {
        get
        {
            return Session["SessionUserName"] == null ? null : Session["SessionUserName"].ToString();
        }
        set
        {
            Session.Add("SessionUserName", value);
        }
    }

    #region 清除所有 Session
    /// <summary>
    /// 清除所有 Session
    /// </summary>
    public void RemoveSession()
    {
        this.Session.RemoveAll();
    }
    #endregion

    #region 取得客戶端真實IP Address
    /// <summary>    
    /// 取得客戶端真實IP Address    
    /// </summary>    
    /// <returns></returns>    
    public string GetIpAddress()
    {
        string strIpAddr = string.Empty;

        if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null || HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].IndexOf("unknown") > 0)
        {
            strIpAddr = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }
        else if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].IndexOf(",") > 0)
        {
            strIpAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Substring(1, HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].IndexOf(",") - 1);
        }
        else if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].IndexOf(";") > 0)
        {
            strIpAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Substring(1, HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].IndexOf(";") - 1);
        }
        else
        {
            strIpAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        }
        return strIpAddr; ;
    }
    #endregion

    #region 設定 Session Timeout
    /// <summary>
    /// 標題：設定 Session Timeout
    /// 備註：
    /// </summary>
    /// <param name="e"></param>
    protected override void OnPreLoad(EventArgs e)
    {
        //base.OnPreLoad(e);

        //CheckSession();

        //try
        //{
        //    Session.Timeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SessionTimeOut"]);
        //}
        //catch
        //{
        //    Session.Timeout = 30;
        //}
    }
    #endregion

    #region 組合錯誤訊息的顯示格式
    /// <summary>
    /// 組合錯誤訊息的顯示格式
    /// </summary>
    /// <param name="aMSG">錯誤訊息</param>
    /// <returns>格式化的錯誤訊息</returns>
    public string ShowErrorMessage(string aMSG)
    {
        string MSG = "<table width=\"100%\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\" class=\"table-top\"><tr>"
            + "<td height=\"24\" valign=\"bottom\" align=\"center\"><font size=\"3\">錯誤訊息通知</font></td></tr></table>"
            + "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" bgcolor=\"#666666\"><tr><td bgcolor=\"#ffffff\">"
            + "<table width=\"100%\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\" bgcolor=\"#506b63\"><tr>"
            + "<td class=\"table-04\" height=\"99\" bgcolor=\"#ffffff\" align=\"center\"><font size=\"5\" color=\"#ff0000\">此為錯誤訊息通知頁，請通知系統管理者，並列印以下錯誤訊息，我們將盡快為您解決</font>"
            + "</td></tr><tr bgcolor=\"#ffffff\"><td class=\"table-04\">" + aMSG + "</td>"
            + "</tr></table></td></tr></table>";
        return MSG;
    }
    #endregion

}
